/*
 * @Descripttion:
 * @version:
 * @Author: kl2018
 * @Date: 2019-03-07 20:06:20
 * @LastEditors: kl2018
 * @LastEditTime: 2020-08-10 14:45:43
 */
import { observable, action } from 'mobx';
import * as Api from './../apis/changePasswordApi';
import { WeaForm } from 'comsMobx';
import { message } from 'antd';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

class ChangePasswordStore {
  @observable form = new WeaForm();
  @observable range = '0';
  // 保存修改密码
  @action.bound savePassword() {
    const formData = this.form.getFormParams();
    if (formData.passwordnew !== formData.conPwd) {
      message.error(getLabel('514406', '两次密码不一样！'));
      return;
    }
    const params = {
      passwordold: formData.passwordold,
      passwordnew: formData.passwordnew,
      validatecode: formData.validatecode,
      validateRand: this.range,
    };
    Api.changePassword(params).then((data) => {
      if (data.status == '1') {
        message.success(data.message);
        this.form.resetForm();
      } else {
        message.error(data.message);
      }
    });
  }
  // 获取修改密码form表单
  @action.bound getFormItem() {
    Api.getFormItem().then((data) => {
      this.form.setCondition(data.items);
    });
  }
  // 校验form
  @action.bound onCheck() {
    this.form.validateForm().then((data) => {
      if (data.isValid) {
        this.savePassword();
      } else {
        data.showErrors();
      }
    });
  }

  @action.bound
  changeVerificationCode() {
    this.range = Math.random();
  }
}
export default new ChangePasswordStore();
